/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemUser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.poemUser.domain.PoemUser;
import me.zhengjie.poemUser.domain.vo.PoemUserQueryCriteria;
import me.zhengjie.poemUser.mapper.PoemUserMapper;
import me.zhengjie.poemUser.service.PoemUserService;
import me.zhengjie.poemmanager.domain.Poem;
import me.zhengjie.poemmanager.domain.vo.QueryCriteria;
import me.zhengjie.poemmanager.mapper.CommentMapper;
import me.zhengjie.poemmanager.mapper.FavoritesMapper;
import me.zhengjie.poemmanager.mapper.ObjectLikeMapper;
import me.zhengjie.poemmanager.util.ExtractUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author hwl
* @date 2024-12-06
**/
@Service
@RequiredArgsConstructor
public class PoemUserServiceImpl extends ServiceImpl<PoemUserMapper, PoemUser> implements PoemUserService {

    private final PoemUserMapper poemUserMapper;
    private final ObjectLikeMapper objectLikeMapper;
    private final CommentMapper commentMapper;
    private final FavoritesMapper favoritesMapper;
    @Override
    public PageResult<PoemUser> queryAll(PoemUserQueryCriteria criteria, Page<Object> page){
        // 获取基础分页数据
        IPage<PoemUser> poemPage =poemUserMapper.findAll(criteria, page);
        return PageUtil.toPage(poemPage);
    }

    @Override
    public List<PoemUser> queryAll(PoemUserQueryCriteria criteria){
        return poemUserMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PoemUser resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PoemUser resources) {
        PoemUser poemUser = getById(resources.getPoemId());
        poemUser.copy(resources);
        saveOrUpdate(poemUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<PoemUser> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PoemUser poemUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", poemUser.getTitle());
            map.put("用户名", poemUser.getAuthorName());
            map.put("主体内容", poemUser.getContent());
            map.put("创造时间", poemUser.getCreatedAt());
            map.put("更新时间", poemUser.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Poem> getMyPoem(String userName) {
        List<Poem>poems=poemUserMapper.getPoemByUserName(userName);
        for (Poem poem:
                poems) {
            String content= ExtractUtil.extrac(poem.getContent()).replaceAll("\\s*|\r|\n|\t","");
            if(content.length()>50)
                content=content.substring(0,50);
            poem.setContent(content);
        }
        return poems;

    }

    @Override
    public PoemUser getPoemById(Integer poemId) {
        PoemUser p=poemUserMapper.getPoemById(poemId);
        p.setComments(commentMapper.getPoemUserCommentByObjectId(poemId));
//        for (Comment c:p.getComments()
//        ) {
//            c.setCountLike(commentMapper.getCountLike(c.getCommentId()));
//        }
        return p;
    }

    @Override
    public List<QueryCriteria> queryByTitle(String title) {
        return poemUserMapper.queryByTitle(title);
    }

    @Override
    public Integer createByUser(PoemUser poem) {
        poemUserMapper.insert(poem);
        return poem.getPoemId();
    }

    @Override
    public List<Poem> getALl() {
        List<Poem>poems=poemUserMapper.getALl();
        for (Poem poem:
                poems) {
            String content= ExtractUtil.extrac(poem.getContent()).replaceAll("\\s*|\r|\n|\t","");
            if(content.length()>50)
                content=content.substring(0,50);
            poem.setContent(content);
        }
        return poems;
    }

    @Override
    public List<Poem> getMyFavoritePoem(String userName) {
        List<Poem>poems=poemUserMapper.getMyFavoritePoem(userName);
        for (Poem poem:
                poems) {
            String content= ExtractUtil.extrac(poem.getContent()).replaceAll("\\s*|\r|\n|\t","");
            if(content.length()>50)
                content=content.substring(0,50);
            poem.setContent(content);
        }
        return poems;
    }

}